<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('银行名称')" prop="bankName">
            <el-input v-model="form.bankName" :placeholder="$t('银行名称')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('授信额度')+$t('金额单位')" prop="creditLine">
            <Money v-model="form.creditLine" :placeholder="$t('授信额度')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('授信有效期')" prop="creditValidity">
            <el-date-picker
              v-model="form.creditValidity"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('授信有效期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('银行联系人信息')" prop="bankContactInformation">
            <el-input v-model="form.bankContactInformation" type="textarea" :placeholder="$t('银行联系人信息')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')" prop="remark">
            <el-input v-model="form.remark" type="textarea" :placeholder="$t('备注')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!--融资记录-->
      <div class="area-header">
        <span class="area-header-title">{{ $t('融资记录') }}</span>
      </div>
      <el-table ref="tableData" :data="form.dealFinancingRecordList" border :show-summary="showSum" :summary-method="getSummaries" class="nowrapTable formTable">
        <el-table-column :label="$t('序号')" align="center" width="50">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('项目名称')" align="center" prop="dealName" width="260" />
        <el-table-column :label="$t('利率模式')" align="center" prop="interestRatePattern" :formatter="interestRatePatternFormat" width="180" />
        <el-table-column :label="$t('利率（%）')" align="center" prop="interestRate" width="180" />
        <el-table-column :label="$t('放款金额（万）')" align="center" prop="lendersMoney" width="180">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.lendersMoney,'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('融资金额（万）')" align="center" prop="amountOfFinancing" width="180">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountOfFinancing,'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('融资摘要')" align="center" prop="financingSummary" width="255" />
      </el-table>

    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="financingLease:dealBank" />
    </div>
  </div>
</template>

<script>
    import { saveDealBankList, getDealBankList } from '@/api/deal/financingLease/dealBank/dealBankList'
    import { moneyFormat } from '@/utils/ruoyi'
    export default {
        data() {
            return {
                // 表单参数
                form: {
                  dealFinancingRecordList: []
                },
                // 利率模式字典
                interestRatePatternOptions: [],
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  bankName: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                showSum: true
            }
        },
        created() {
          this.getDicts('interest_rate_pattern').then(response => {
            this.interestRatePatternOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealBankList(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    bankName: undefined,
                    creditLine: undefined,
                    creditValidity: undefined,
                    bankContactInformation: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealBankList(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           },
          // 利率模式字典翻译
          interestRatePatternFormat(row, column) {
            console.log(column)
            return this.selectDictLabel(this.interestRatePatternOptions, row.interestRatePattern)
          },
          // 合计
          getSummaries(param) {
            const {
              columns,
              data
            } = param
            const sums = []
            columns.forEach((column, index) => {
              if (index === 0) {
                sums[index] = '合计'
                return
              } else if (index === 4 || index === 5) {
                const values = data.map(item => Number(item[column.property]))
                if (!values.every(value => isNaN(value))) {
                  sums[index] = values.reduce((prev, curr) => {
                    const value = Number(curr)
                    if (!isNaN(value)) {
                      return moneyFormat(prev + curr, 'w')
                    } else {
                      return moneyFormat(prev, 'w')
                    }
                  }, 0)
                  sums[index] += '万'
                } else {
                  sums[index] = 'N/A'
                }
              } else {
                sums[index] = ''
              }
            })
            return sums
          }
        }
    }
</script>
